﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace Circa {

    public class PageItem {
        public string Caption { get; set; }
        public FrameworkElement Content { get; set; }
        public Page Page { get; set; }

        public override bool Equals( object o ) {
            if ( o is PageItem )
                return Page.Equals( ( (PageItem) o ).Page );
            return false;
        }

        public override int GetHashCode( ) {
            return Page.GetHashCode( );
        }

    }

    public class PageCollection: ObservableCollection<PageItem> {
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow: Window {

        private PageCollection pages = new PageCollection( );
        private ConsolePage console = new ConsolePage( );

        public int AddPage( string caption, Page page ) {
            PageItem pageitem = new PageItem {
                Caption = caption,
                Content = new Frame {
                    Focusable = false,
                    Content = page,
                },
                Page = page,
            };

            int newIndex;
            lock ( pages ) {
                newIndex = pages.Count;
                pages.Add( pageitem );
            }
            tabControl.SelectedIndex = newIndex;

            return newIndex;
        }

        public void RemovePage( int index ) {
            pages.RemoveAt( index );
        }

        public void RemovePage( Page page ) {
            pages.Remove( new PageItem { Page = page } );
        }

        public MainWindow( ) {
            InitializeComponent( );
            tabControl.DataContext = pages;
            
            AddPage( "Console", console );
        }

    }

}
